package com.care.wardmate.server.service.impl;

import com.care.common.security.utils.SecurityUtils;
import com.care.wardmate.common.domain.Review;
import com.care.wardmate.server.mapper.ReviewMapper;
import com.care.wardmate.server.service.ArticleService;
import com.care.wardmate.server.service.ReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author 33253
 */
@Service
public class ReviewServiceImpl implements ReviewService {
    @Autowired
    private ReviewMapper reviewMapper;
    @Autowired
    private ArticleService articleService;

    @Override
    public List<Review> selectReviewById(Integer articleId) {
        List<Review> list = reviewMapper.selectReviewById(articleId);
        List<Review> collect = list.stream ()
                .filter (review -> {
                    return review.getParentId () == 0;
                })
                .map (review -> {
                    review.setChildren (getChildren (list, review).size () == 0 ? null : getChildren (list, review));
                    return review;
                })
                .collect (Collectors.toList ());


        return collect;
    }

    private List<Review> getChildren(List<Review> list, Review review) {
        List<Review> collect = list.stream ()
                .filter (review1 -> {
                    return Objects.equals (review1.getParentId (), review.getReviewId ());
                })
                .map (review1 -> {
                    review1.setChildren (getChildren (list, review1).size () == 0 ? null : getChildren (list, review1));
                    return review1;
                })
                .collect (Collectors.toList ());
        return collect;
    }


}
